<?php

namespace app\admin\controller;


use app\common\base\AuthenticatedController;
use app\common\base\Form;
use think\Db;
use think\Error;


/**
 * 收费项目控制器
 * 这里代码没用
 * Class DeviceItemController
 * @package app\admin\controller
 */
class DeviceItemController extends AuthenticatedController {


    public function Add() {

        Form::Validate([
            'name'                  =>  'require',
            'price'                 =>  'require|integer',
            'device_id'             =>  'require|integer',
        ]);

        $data = [
            'name'                  =>  input('name'),
            'price'                 =>  input('price'),
            'device_id'             =>  input('device_id'),
        ];

        $result = Db::table('device_item')->insert($data);
        if (!$result) {
            return Error('添加失败');
        }

        return Success('添加设备成功');

    }


    public function Delete() {

        Form::Validate([
            'id'  =>  'require',
        ]);

        if (!Db::table('device_item')->where('id', input('id'))->delete()) {
            return Error('删除失败');
        }

        return Success('删除成功');

    }


    public function Modify() {

        Form::Validate([
            'id'                    =>  'require|integer',
            'name'                  =>  'require',
            'price'                 =>  'require|integer',
            'device_id'             =>  'require|integer',
        ]);

        $data = [
            'name'                  =>  input('name'),
            'price'                 =>  input('price'),
            'device_id'             =>  input('device_id'),
        ];

        if (Db::table('device_item')->where('id', input('id'))->update($data) === false) {
            return Error('更新失败');
        }

        return Success('更新成功');
    }


    public function GetList() {
        Form::Validate([
            'device_id'     =>  'require|integer'
        ]);

        $result = Db::table('device_item')->where('device_id', input('device_id'))->select();

        return Success('获取成功', $result);
    }


    public function GetOne() {
        Form::Validate([
            'id' => 'require|integer'
        ]);

        $result = Db::table('device_item')->where('id', input('id'))->find();
        if (!$result) {
            return Error('获取失败');
        }

        return Success('获取成功', $result);


    }

}